package com.example.demo.service.impl;

import com.example.demo.entity.Personnelmanageinitialization;
import com.example.demo.handle.InitializationListParseHandler;
import com.example.demo.mapper.PersonnelmanageinitializationMapper;
import com.example.demo.service.PersonnelmanageinitializationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-21
 */
@Service
public class PersonnelmanageinitializationServiceImpl extends ServiceImpl<PersonnelmanageinitializationMapper, Personnelmanageinitialization> implements PersonnelmanageinitializationService {

    @Autowired
    private PersonnelmanageinitializationMapper personnelmanageinitializationMapper;

    @Override
    public Integer insertInitializationData(String initializationXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            InitializationListParseHandler initializationListParseHandler = new InitializationListParseHandler();
            try {
                //读取井口信息文件
                parser.parse(initializationXml, initializationListParseHandler);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + initializationListParseHandler.getPersonnelmanageinitializationArrayList().size() + "个信息");
            // 根据minecode去查找数据库
            for (Personnelmanageinitialization personnelmanageinitialization : initializationListParseHandler.getPersonnelmanageinitializationArrayList()) {
                System.out.println(personnelmanageinitialization.getCsMineCode());
                System.out.println(personnelmanageinitialization.getCsDataTime());
                System.out.println(personnelmanageinitialization.getPsSystemModel());
                System.out.println(personnelmanageinitialization.getPsSystemName());
                System.out.println(personnelmanageinitialization.getPsSystemMaker());
                System.out.println(personnelmanageinitialization.getPsSystemProdate());
                System.out.println(personnelmanageinitialization.getPsSystemManum());
                System.out.println(personnelmanageinitialization.getPsSystemMaeffec());
                System.out.println(personnelmanageinitialization.getPsSystemInstime());
                System.out.println(personnelmanageinitialization.getPsPersonsAllow());
                System.out.println(personnelmanageinitialization.getPsAllowMaxtime());
                System.out.println(personnelmanageinitialization.getPsWorkTime());

                System.out.println("----------------------------安全监控信息finish---------------------------------");

                personnelmanageinitializationMapper.insertInitializationData(personnelmanageinitialization.getCsMineCode(),personnelmanageinitialization.getCsDataTime(),personnelmanageinitialization.getPsSystemModel(),personnelmanageinitialization.getPsSystemName(),personnelmanageinitialization.getPsSystemMaker(),personnelmanageinitialization.getPsSystemProdate(),personnelmanageinitialization.getPsSystemManum(),personnelmanageinitialization.getPsSystemMaeffec(),personnelmanageinitialization.getPsSystemInstime(),personnelmanageinitialization.getPsPersonsAllow(),personnelmanageinitialization.getPsPersonsAllow(),personnelmanageinitialization.getPsWorkTime());
            }


        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
